// pages/course/course.js
const API = require('../../http/API')
const http = require('../../http/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option: [
      {
        text: "最新"
      },
      {
        text: "入门",
        level: "1"
      },
      {
        text: "进阶",
        level: "2,3,4"
      },
      {
        text: "免费"
      },
      {
        text: "图文"
      },
      {
        text: "兑换"
      },
      {
        text: "专题"
      },
    ],
    currentIndex: 0,
    subjects: [],
    subjectsIndex: 0,
    isShowMore: false,
    limit: 10,
    page: 1,
    courseList: [],
    subjectsId: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.courseGroup()
    this.getCourse()
  },
  /**
   * 事件处理
   */
  // 获取课程数据
  getCourse(id) {
    if (id) {
      http(API.courseVideo, {
        type: 1,
        page: this.data.page,
        limit: this.data.limit,
        course_subjects_ids: id
      }).then(res => {
        if (this.data.page === 1) {
          this.setData({ courseList: res.data.data })
        } else {
          this.setData({ courseList: this.data.courseList.concat(res.data.data) })
        }
      })
    } else {
      http(API.courseVideo, {
        type: 1,
        page: this.data.page,
        limit: this.data.limit,
      }).then(res => {
        if (this.data.page === 1) {
          this.setData({ courseList: res.data.data })
        } else {
          this.setData({ courseList: this.data.courseList.concat(res.data.data) })
        }
      })
    }
  },
  // 点击 subjects-name
  nameTap(e) {
    this.setData({ subjectsIndex: e.currentTarget.dataset.index })
    this.getCourse(e.currentTarget.id)
    this.setData({ subjectsId: e.currentTarget.id })
  },
  moreTap() {
    this.setData({ isShowMore: !this.data.isShowMore })
  },
  // 获取courseGroup数据
  courseGroup() {
    http(API.courseGroup).then(res => {
      this.setData({ subjects: res.data.course_subjects })
    })
  },
  // 点击轮播图
  optionTap(e) {
    this.setData({ currentIndex: e.currentTarget.dataset.index })
  },
  confirmTap(e) {
    console.log(e.detail.value);
    http(API.courseVideo, {
      type: 1,
      page: this.data.page,
      limit: this.data.limit,
      title: e.detail.value
    }).then(res => {
      if (this.data.page === 1) {
        this.setData({ courseList: res.data.data })
      } else {
        this.setData({ courseList: this.data.courseList.concat(res.data.data) })
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({ page: this.data.page + 1, })
    this.getCourse()
  },
})